package servlet;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Conn;
import dao.UserDAO;

import bean.User;

public class LoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String sAddress = "index.jsp";
		String sName = req.getParameter("username");
		String sPassword = req.getParameter("password");
		User user = new User();
		Connection conn = Conn.getConn();
		user = UserDAO.getAUser(conn, sName);
		if(sPassword.equals(user.getUPassword())){
			if(user.getULoginFlag().equals("N")){
				if(UserDAO.updateLoginFlag(conn, sName)){
					HttpSession session = req.getSession();
					session.setAttribute("user", user);
				}			
			}
			else {
				sAddress = "loggedin.jsp";
			}
		}
		Conn.close(conn);
		resp.sendRedirect(sAddress);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}

}
